package cn.cdeden.knowledge.controller.personalChat;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.PersonalChatHistory;
import cn.cdeden.knowledge.domain.dto.ChatHistoryDto;
import cn.cdeden.knowledge.domain.dto.PersonalChatHistoryDto;
import cn.cdeden.knowledge.service.PersonalChatHistoryService;
import cn.cdeden.system.api.model.LoginUser;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 个人对话历史记录模块
 */

@RestController
@RequestMapping("/personal/chat/history")
@RequiredArgsConstructor
public class PersonalChatHistoryController {

    private final PersonalChatHistoryService personalChatHistoryService;

    /**
     *保存或者修改普通问答历史记录
     * @param personalChatHistoryDto
     * @return
     */
    @PostMapping("/add/or/edit")
    public R<PersonalChatHistory> addPersonalChatHistory(@RequestBody PersonalChatHistoryDto personalChatHistoryDto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        String objects = JSONUtil.toJsonStr(personalChatHistoryDto.getChatHistoryDtoList());
        PersonalChatHistory personalChatHistory = new PersonalChatHistory();
        if (personalChatHistoryDto.getChatId() != null) {
            personalChatHistory.setChatId(personalChatHistoryDto.getChatId());
        }
        personalChatHistory.setTitle(personalChatHistoryDto.getChatHistoryDtoList().get(0).getContent());
        personalChatHistory.setContent(objects);
        if (personalChatHistoryDto.getChatId() == null) {
            personalChatHistory.setCreateTime(new Date());
        }
        personalChatHistory.setUserId(loginUser.getUserId());
       return R.ok(personalChatHistoryService.addOrEdit(personalChatHistory));
    }

    /**
     * 查询当前人员的历史对话
     * @return
     */
    @GetMapping("/get")
    public R<List<PersonalChatHistory>> getPersonalChatHistory() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<PersonalChatHistory> personalChatHistories = personalChatHistoryService.list(new LambdaQueryWrapper<PersonalChatHistory>()
            .eq(PersonalChatHistory::getUserId, loginUser.getUserId()).orderByAsc(PersonalChatHistory::getCreateTime));
        return R.ok(personalChatHistories);
    }

    /**
     * 根据id查询对话的历史内容
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public R getPersonalChatHistoryById(@PathVariable Long id) {
        PersonalChatHistory personalChatHistory = personalChatHistoryService.getById(id);
        PersonalChatHistoryDto personalChatHistoryDto = new PersonalChatHistoryDto();
        personalChatHistoryDto.setChatId(personalChatHistory.getChatId());
        personalChatHistoryDto.setTitle(personalChatHistory.getTitle());
        personalChatHistoryDto.setUserId(personalChatHistory.getUserId());
        personalChatHistoryDto.setChatHistoryDtoList(JSONUtil.toList(personalChatHistory.getContent(), ChatHistoryDto.class));
        return R.ok(personalChatHistoryDto);
    }


    /**
     * 清空当前用户的所有普通问答历史
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteAllHistory() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        personalChatHistoryService.remove(new LambdaQueryWrapper<PersonalChatHistory>()
            .eq(PersonalChatHistory::getUserId, loginUser.getUserId()));
        return R.ok();
    }

    /**
     * 根据id删除单个历史对话
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        personalChatHistoryService.remove(new LambdaQueryWrapper<PersonalChatHistory>()
            .eq(PersonalChatHistory::getChatId, id));
        return R.ok();
    }


}
